package com.leejxx.health.controller;

import com.leejxx.health.common.MessageConst;
import com.leejxx.health.entity.PageResult;
import com.leejxx.health.entity.QueryPageBean;
import com.leejxx.health.entity.Result;
import com.leejxx.health.pojo.Role;
import com.leejxx.health.service.RoleService;
import com.leejxx.health.vo.AddOrEditRoleParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author LeeJx
 * @ClassName RoleController
 * @Description TODO
 * @date 2021/1/11 19:53
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/role")
public class RoleController {
    @DubboReference
    private RoleService roleService;

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("[角色管理-分页查询]data:{}", queryPageBean);
        PageResult pageResult = roleService.findByPage(queryPageBean);
        log.info("[角色管理-分页查询成功]data:{}", pageResult);
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    @RequestMapping("/findPermissionAndMenu")
    public Result findPermissionAndMenu() {
        Role permissionsAndMenus = roleService.findPermissionsAndMenus();
        log.info("[角色管理-新增角色权限和菜单回显成功]data:{}", permissionsAndMenus);
        return new Result(true, MessageConst.ACTION_SUCCESS, permissionsAndMenus);
    }

    @RequestMapping("/add")
    public Result add(@RequestBody AddOrEditRoleParam roleParam) {
        log.info("[角色管理-新增角色]data:{}", roleParam);
        roleService.addRole(roleParam.getRole(), roleParam.getPermissionIds(), roleParam.getMenuIds());
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/findRoleById")
    public Result findRoleById(Integer roleId) {
        log.info("[角色管理-修改回显]data:{}", roleId);
        Role role = roleService.findRoleById(roleId);
        log.info("[角色管理-修改回显成功]data:{}", role);
        return new Result(true, MessageConst.ACTION_SUCCESS, role);
    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody AddOrEditRoleParam roleParam) {
        log.info("[角色管理-修改角色]data:{}", roleParam);
        roleService.edit(roleParam.getRole(), roleParam.getPermissionIds(), roleParam.getMenuIds());
        log.info("[角色管理-修改角色成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/delete")
    public Result delete(Integer roleId) {
        log.info("[角色管理-删除角色]data:{}", roleId);
        roleService.deleteById(roleId);
        log.info("[角色管理-删除角色成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }
}
